﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Service.Day
{
    /// <summary>
    /// 账户日持仓汇总
    /// </summary>
    public class ServiceAccountDayPositionDelivery : Benefit.Interface.Day.IAccountDayPositionDelivery
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayPositionDelivery(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        public void InitAccountDayPositionDelivery(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.Sys.IAccount iaccount = new Sys.ServiceAccount(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {

                //Sys.Account _account = new Sys.Account();
                int acountid = iaccount.GetAccountId(account.LoginAccount);
             //   Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                var query = db.PositionDetailHistory.Where(a => a.AccountId == account.Id).Where(a => a.TradingDay.Equals(pdate));
                foreach (Models.ServerData.T_PositionDetail_History s in query)
                {
                    Models.Day.AccountDayPositionDelivery change = new Models.Day.AccountDayPositionDelivery();
                    change.AccountId = acountid;
                    change.Brokerid = s.Brokerid;
                    change.CloseAmount = s.CloseAmount;
                    change.ExchangeId = s.ExchangeId;
                    change.Exchmargin = s.Exchmargin;
                    change.CloseProfitByDate = s.CloseProfitByDate;
                    change.CloseProfitByTrade = s.CloseProfitByTrade;
                    change.CloseVolume = s.CloseVolume;
                    change.CombinStrumentId = s.CombinStrumentId;
                    change.Commission = s.Commission;
                    change.Direction = s.Direction;
                    change.FloatingProfit = s.FloatingProfit;
                    change.HedgeFlag = s.HedgeFlag;
                    change.InstrumentId = s.InstrumentId;
                    change.Investorid = s.Investorid;
                    change.Margin = s.Margin;
                    change.MarginRateByMoney = s.MarginRateByMoney;
                    change.MarginRateByVolume = s.MarginRateByVolume;
                    change.OpenDate = s.OpenDate;
                    change.OpenPrice = s.OpenPrice;
                    change.OpenTime = s.OpenTime;
                    change.PositionProfit = s.PositionProfit;
                    change.PositionProfitByDate = s.PositionProfitByDate;
                    change.PositionProfitByTrade = s.PositionProfitByTrade;
                    change.ProductId = s.ProductId;
                    change.SettleMentPrice = s.SettleMentPrice;
                    change.TradeHistoryId = tradeHistoryId;
                    change.TradeId = s.TradeId;
                    change.TradeType = s.TradeType;
                    change.TradingDay = s.TradingDay;
                    change.Volume = s.Volume;
                    change.YesterdaySettlement = s.YesterdaySettlement;
                    db.AccountDayPositionDelivery.Add(change);
                }
            }
            db.SaveChanges();
        }

        public List<Models.Day.AccountDayPositionDelivery> GetAccountDayPositionDelivery(int accountid, int tradeHistoryId)
        {
            var query = db.AccountDayPositionDelivery.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.AccountId == accountid).ToList();
            return query;
        }

        public void Delete(int tradeHistoryId)
        {
            var query = db.AccountDayPositionDelivery.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (Models.Day.AccountDayPositionDelivery del in query)
            {
                db.AccountDayPositionDelivery.Remove(del);
            }
            db.SaveChanges();
        }
    }
}
